package com.link.base.tencent.corpwx.media.controller;

import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import com.link.core.cllog.LCLogger;
import com.link.base.tencent.corpwx.media.model.CorpWxMedia;
import com.link.base.tencent.corpwx.media.service.CorpWxMediaService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 企业微信素材管理
 *
 * @author 李宗庭
 * @date 2019/1/2
 */
@Controller
@RequestMapping("/link/corpwx/media")
public class CorpWxMediaController extends BasicController<CorpWxMedia> {

    @Resource
    private CorpWxMediaService corpWxMediaService;

    @Override
    public BasicService<CorpWxMedia> getBasicService() throws Exception {
        return corpWxMediaService;
    }

    /**
    * 下载企业微信临时素材 存到服务器
    * @author 李宗庭
    * @date 2019/1/26
    */
    @GetMapping(value = "/downloadMedia", name = "下载企业微信临时素材")
    @ResponseBody
    public Map<String, Object> downloadMedia(HttpSession session,
                                            HttpServletRequest request, HttpServletResponse response) {
        LCLogger.info().withMessageKey("downloadMedia").withMessage("开始下载临时素材.").flush();
        Map<String, Object> result = new HashMap<String, Object>(4);

        String parentId = request.getParameter("parentId");
        String module = request.getParameter("module");
        String agentId = request.getParameter("agentId");
        String mediaId = request.getParameter("mediaId");
        try {
            CorpWxMedia record = new CorpWxMedia();
            record.setModuleType(module);
            record.setHeadId(Long.parseLong(parentId));
            record.setAgentId(agentId);
            record.setMediaId(mediaId);
            record = corpWxMediaService.downloadMedia(record);
            result.put("result", record);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }
    
    /** 
    * 上传临时素材到企业微信，并保存到服务器
    * @author Lizongting
    * @date 2020/1/15
    */
    @PostMapping(value = "/uploadMedia", name = "上传临时素材到企业微信")
    @ResponseBody
    public Map<String, Object> uploadMedia(CorpWxMedia entity, HttpSession session,
                                             HttpServletRequest request, HttpServletResponse response) {
        LCLogger.info().withMessageKey("uploadMedia").withMessage("开始上传临时素材到企业微信.").flush();
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            CorpWxMedia record = corpWxMediaService.uploadMedia(request, entity);
            result.put("result", record);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

}
